<?php
namespace Project_725;

use TwinPeaks\Core as Core;
use TwinPeaks\Core\Http as Http;
use Zend\Db\Adapter\Adapter;


require_once __DIR__ . '/../../framework/core/CWebApplication.php';

class AppProject725 extends Core\CWebApplication
{
	/**
	 * @return string
	 */
	function getFrameworkDirectory()
	{
		return realpath(__DIR__ . '/../../framework');
	}
	
	/**
	 * @return string
	 */
	function getAppDirectory()
	{
		return __DIR__;
	}
	
	/**
	 * @return string
	 */
	function getAppNamespace()
	{
		return __NAMESPACE__;
	}
	
	
	/**
	 * @return AppProject_725
	 */
	function initialize()
	{
		parent::initialize();
		
		$this->definePathAlias('frontend', $this->getAppDirectory() . '/View/Frontend');
		$this->definePathAlias('backend', $this->getAppDirectory() . '/View/Backend');
		
		$this->registerNamespace('Project_725\Lib', '@A/Lib/');
		$this->registerNamespace('Project_725\Model', '@A/Model/');	
		$this->registerNamespace('Zend', '@vendor/zf2/library/Zend/');
		
		if (strpos($this->getRequestURI()->getHost(), '.local'))
		{
			$this->getConfig()->applyFromFile('@A/debug.conf.php');
		}

		Http\CUri::setDefaultHost($this->getConfig()->domain_name);
		return $this;
	}
	
	/**
	 * @return CFrontController 
	 */
	function selectFrontController($requestURI)
	{	
		if (preg_match('|/api|', $requestURI->getPath()))
		{
			$fcInstance = FC_Api::getInstance();
		}
		else
		{
			$fcInstance = FC_Frontend::getInstance();
		}
		
		return $fcInstance;
	}
	
	/**
	 * @return void
	 */
	function defineNodes()
	{
		$node = new Core\CNode('localhost');
		
		$node->addService(self::MEMCACHE_SERVICE_ID)->setPort(12211);
		
		$this->addNode($node);
	}
	
	function errPageNotFound()
	{
		parent::errPageNotFound();
		echo 'sdf';
	}
	
	/**
	 * @return void
	 */
	protected function errAuthRequired()
	{
		Location::loginPage()->_goto();
	}
	
	/**
	 * @return \Zend\Db\Adapter\Adapter
	 */
	function getDbAdapter()
	{
		static $adapter = null;
		
		if (null != $adapter)
			return $adapter;
		
		$conf = $this->getConfig();
		
        return null;
		$adapter = new Adapter([
			'driver' => 'PDO_PGSQL',
			'database' => $conf->dbName,
			'username' => $conf->dbUser,
			'password' => $conf->dbPass
		]);
		
		$adapter->query('set SCHEMA \'' . $conf->dbSchema . '\'', Adapter::QUERY_MODE_EXECUTE);		
		\Zend\Db\TableGateway\Feature\GlobalAdapterFeature::setStaticAdapter($adapter);
		return $adapter;
	}
	
	/**
	 * @return \Zend\Db\Adapter\Driver\ConnectionInterface
	 */
	protected function getDbConn()
	{
		//return $this->getDbAdapter()->driver->getConnection();
	}
	
	/**
	 * @return void
	 */
	function preRuleExecution()
	{
		//$this->getDbConn()->beginTransaction();
	}
	
	/**
	 * @return void
	 */
	function postRuleExecution()
	{
		//$this->getDbConn()->commit();
	}
	
	/**
	 * @return void
	 */
	function onPDOException($excep)
	{
		//$this->getDbConn()->rollback();
	}
	
	/**
	 * @return void
	 */
	function onPageNotFound()
	{
		$layout = View\Frontend\Layout::getInstance();
		$this->getResponse()->setBody($layout->render());
		parent::onPageNotFound();
	}

	/**
	* @return void
	*/
	function onPHPFatalError()
	{
		if (!$this->getConfig()->debugMode)
		{
			$layout = View\Frontend\Layout::getInstance();
			$this->getResponse()
				->setBody($layout->render())
				->dispatch();
		}
	}
}
